package com.jeecg.demo.controller;

import com.jeecg.demo.entity.*;
import com.jeecg.demo.service.*;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**   
 * @Title: Controller  
 * @Description:
 * @author onlineGenerator
 * @date
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/vfreezeDepositController")
public class VFreezeDepositController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(VFreezeDepositController.class);
	
	@Autowired
	private VFreezeDepositServiceI vfreezeDepositService;
	@Autowired
	private PostorderServiceI postorderService;
	@Autowired
	private TraderServiceI traderService;
	@Autowired
	private SysAppRegionServiceI sysAppRegionService;
	@Autowired
	private VEcpPostorderServiceI vecpPostorderService;

	/**
	 *
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/jeecg/freeze/freezeDepositList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(VFreezeDepositEntity vfreezeDeposit, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(VFreezeDepositEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, vfreezeDeposit, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.vfreezeDepositService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(VFreezeDepositEntity freezeDeposit, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(freezeDeposit.getId())) {
			freezeDeposit = vfreezeDepositService.getEntity(VFreezeDepositEntity.class, freezeDeposit.getId());
			req.setAttribute("freezeDepositPage", freezeDeposit);
		}
		return new ModelAndView("com/jeecg/freeze/freezeDeposit-update");
	}

	@RequestMapping(params = "goPostorder")
	public ModelAndView goPostorder(VFreezeDepositEntity freezeDeposit, HttpServletRequest req) {

		VFreezeDepositEntity tempEntity = vfreezeDepositService.get(VFreezeDepositEntity.class, freezeDeposit.getId());

		PostorderEntity postorder= new PostorderEntity();
		//String pocode = req.getParameter("pocode");
		List<PostorderEntity> templist = postorderService.findHql("from PostorderEntity where pocode = ?",tempEntity.getPocode());
		if (templist.size() > 0) {
			postorder = templist.get(0);
		}

		if (StringUtil.isNotEmpty(postorder.getId())) {
			postorder = postorderService.getEntity(PostorderEntity.class, postorder.getId());

			TraderEntity trader = null;
			List<TraderEntity> list5 = traderService.findHql(
					"from TraderEntity where traderid = ?", postorder.getPotraderid());
			if (list5.size() > 0) {
				trader = list5.get(0);
			}

			SysAppRegionEntity region = null;
			List<SysAppRegionEntity> list4 = sysAppRegionService.findHql(
					"from SysAppRegionEntity where regioncode = ? ", postorder.getDelivercity());
			if (list4.size() > 0) {
				region = list4.get(0);
			}

			Commodity commodity = null;
			SysAppRegionEntity regionChd = null;
			List<Commodity> list = sysAppRegionService.findHql(
					"from Commodity where commodityid = ? ", postorder.getCommodityid());
			if (list.size() > 0) {
				commodity = list.get(0);
				List<SysAppRegionEntity> list2 = sysAppRegionService.findHql(
						"from SysAppRegionEntity where regioncode = ? ", commodity.getChdregioncode());
				if (list2.size() > 0) {
					regionChd = list2.get(0);
				}
			}

			VEcpPostorderEntity vcepPostorder = null;
			List<VEcpPostorderEntity> list3 = vecpPostorderService.findHql(
					"from VEcpPostorderEntity where poid = ?", postorder.getPoid());
			if (list3.size() > 0) {
				vcepPostorder = list3.get(0);
			}

			String imageServerUrl = ResourceUtil.getConfigByName("imageServerUrl");

			req.setAttribute("action", "postorderController.do?doUpdate");
			req.setAttribute("postorderPage", postorder);
			req.setAttribute("trader", trader);
			req.setAttribute("region", region);
			req.setAttribute("commodity", commodity);
			req.setAttribute("chdregion", regionChd);
			req.setAttribute("vecppostorder", vcepPostorder);
			req.setAttribute("imageServerUrl", imageServerUrl);
			req.setAttribute("flags", req.getParameter("type"));
		}
		return new ModelAndView("com/jeecg/demo/postorder-update");
	}
	
}
